const disease = require('../models/disease');
const diseaseType = require('../models/diseaseType')
const Sequelize = require('sequelize');
const Op = Sequelize.Op;

class diseaseDao {
  // 创建
  static async create(v) {
    const res = await disease.create(
      v
    )
    return res
  }
  // 获取单条信息
  static async findById(v) {
    const res = await disease.findOne({
      where: {
        id: v
      },
    });
    return res
  }
  // 获取所有数据(分页)
  static async findAll(v) {
    const res = await disease.findAndCountAll({
      limit: v.limit,
      offset: v.offset
    })
    let result = {}
    result.data = res.rows;
    result.total = res.count;
    return result
  }
  // 删除单条信息
  static async delById(v) {
    const res = await disease.destroy({
      where:{
        id:v
      }
    })
    return res
  }
  // 新增浏览
  static async addPv(v) {
    console.log('-------');
    const res = await disease.increment(['pv'], {
      where: {
        id: v
      }
    });
    console.log(res);
    return res
  }
  // 修改某条数据
  static async upd(v) {
    const res = await disease.update(v, {
      where: {
        id: v.id
      }
    })
    return res
  }
  // getByType
  static async getByType(v){
    const diseaseObj = await diseaseType.findOne({
      where: {
        id: v
      },
    });
    console.log(diseaseObj)
    let res = await diseaseObj.getDiseaseTypes()
    return res
  }
  // 获取大家在看
  static async getMostPV(v) {
    let limit = 10;
    let offset = 0;
    const res = await disease.findAll({
      order: [
        ['pv', 'DESC']
      ],
      limit: limit,
      offset: offset
    })
    return res
  }
  // 建立关联
  static async relate(v) {
    
  }
}

module.exports = diseaseDao